package com.hundsun.fcp.webplugin.widget.webview;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hundsun.fcp.webplugin.FundTradingFunction;
import com.hundsun.fcp.webplugin.FundTradingHelper;
import com.hundsun.fcp.webplugin.javascript.WebHtmlProxy;
import com.hundsun.fcp.webplugin.util.SdCardUtil;

public class FcpWebView extends WebView {
	private Context mContext;
	private IWebViewDelegator mDelegator;
	private String htmlPath;

	public FcpWebView(Context context) {
		super(context);
		init(context);
	}

	public FcpWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	private void init(Context pContext) {
		mContext = pContext;
		if (!isInEditMode()) {
			setWebChromeClient(new FcpWebChromeClient());
			setWebViewClient(new FcpWebViewClient(mContext));

		}
	}

	@SuppressLint("NewApi")
	public void initWebViewInfo() {
		setScrollBarStyle(ACCESSIBILITY_LIVE_REGION_NONE);
		WebSettings _WebSettings = getSettings();
		// 提高渲染的优先级
		_WebSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
		_WebSettings.setLoadWithOverviewMode(true);
		_WebSettings.setJavaScriptEnabled(true);
		_WebSettings.setCacheMode(2);
		_WebSettings.setAllowFileAccess(true);
		_WebSettings.setAppCacheEnabled(true);
		_WebSettings.setAppCacheMaxSize(10 * 1024 * 1024);
		String appCaceDir = this.getContext().getDir( "cache" , Context.MODE_PRIVATE).getPath();
		_WebSettings.setAppCachePath(appCaceDir) ;
		_WebSettings.setDomStorageEnabled(true);
		_WebSettings.setDatabaseEnabled(true);
		_WebSettings.setDatabasePath(this.mContext.getApplicationContext()
				.getDir("fcd-web-db", Context.MODE_PRIVATE).getPath());
		clearCache(true);
		if (Build.VERSION.SDK_INT >= 16) {
			_WebSettings.setAllowUniversalAccessFromFileURLs(true);
		}
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
		    this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		}
		setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);
		addJavascriptInterface(new WebHtmlProxy(this), "webproxy");
	}

	public void alertAndQuitSdk(String pMessage) {
		AlertDialog.Builder _Builder = new AlertDialog.Builder(this.mContext);
		_Builder.setMessage(pMessage);
		_Builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface pInterface,int pInt) {
				pInterface.cancel();
				if (hasDelegator()) {
					mDelegator.quitSdk(FcpWebView.this);
				}
				if (getContext() instanceof Activity) {
					clearLocalStorage();
					((Activity) Activity.class.cast(getContext())).finish();
				}
			}
		});
		AlertDialog localAlertDialog = _Builder.create();
		localAlertDialog.setCanceledOnTouchOutside(false);
		localAlertDialog.show();
	}
	
	 public void loadPluginPage(FundTradingFunction pFundTradingFunction)
	  {
	    String tradingPageFileName = FundTradingHelper.getTradingPageFileName(pFundTradingFunction);
	    if (tradingPageFileName == null) {
	      return;
	    }
	    htmlPath = SdCardUtil.locateHtmlFilePath(this.mContext);
	    if (this.htmlPath.startsWith(SdCardUtil.AndroidAssetHtmlNotExist))
	    {
	      alertAndQuitSdk("未检测到SD卡，请检查设置");
	      return;
	    }
	    clearLocalStorage();
	    loadUrl(this.htmlPath + tradingPageFileName);
	  }

	@Override
	public void goBack() {
		if (super.canGoBack()) {
			clearLocalStorage();
			super.goBack();
		}

		if (hasDelegator()) {
			mDelegator.goBack(this);
		}

	}

	@Override
	public boolean canGoBack() {
		boolean flag = super.canGoBack();
		if ((hasDelegator()) && (!flag)) {
			flag = this.mDelegator.canGoBack(this);
		}
		return flag;
	}
	

	public void setWebChromeClient(WebChromeClient pWebChromeClient) {
		super.setWebChromeClient(pWebChromeClient);
	}

	public void setWebViewClient(WebViewClient pWebViewClient) {
		if (pWebViewClient instanceof FcpWebViewClient) {
			super.setWebViewClient(pWebViewClient);
		}
	}

	public void clearLocalStorage() {
		loadUrl("javascript:;");
	}

	public boolean hasDelegator() {
		return this.mDelegator != null;
	}

	public void setWebViewDelegator(IWebViewDelegator pIWebViewDelegator) {
		this.mDelegator = pIWebViewDelegator;
	}

	public static abstract interface IWebViewDelegator {
		public abstract boolean canGoBack(FcpWebView pFcpWebView);

		public abstract void goBack(FcpWebView pFcpWebView);

		public abstract void quitSdk(FcpWebView pFcpWebView);
	}
}
